﻿@using FujiyBlog.Core.DomainObjects
@using FujiyBlog.Web.Extensions
@using FujiyBlog.Core.Extensions

@model FujiyBlog.Web.Areas.Admin.ViewModels.AdminPageSave

@{
    Layout = MVC.Admin.Shared.Views._Layout;
    ViewBag.Title = "Edit";
    string uploadFolder = "Page" + (Model.Id.HasValue ? Model.Id.ToString() : Guid.NewGuid().ToString());
}
<script type="text/javascript" src="@Url.Content(Links.Scripts.tinymce.jquery_tinymce_js)"></script>

<h2>@(Model.Id.HasValue ? "Edit" : "Add") page</h2>

@Html.Partial("UploadForms", uploadFolder)

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Page</legend>

        @Html.HiddenFor(model => model.Id)
        
        <div class="edit-content-left">

            <div class="editor-wrapper-title">
                <div class="editor-label">
                    @Html.LabelFor(model => model.Title)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Title)
                    @Html.ValidationMessageFor(model => model.Title)
                </div>
            </div>
            
            <div class="editor-wrapper-slug">
                <div class="editor-label">
                    @Html.LabelFor(model => model.Slug)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Slug) <a href="javascript:void(0)" id="extract-from-title">Extract from title</a>
                    @Html.ValidationMessageFor(model => model.Slug)
                </div>
            </div>

            <div class="upload-links">
                <a href="javascript:void(0)" id="link-upload-image">Insert image</a>
                <a href="javascript:void(0)" id="link-upload-video">Insert video</a>
                <a href="javascript:void(0)" id="link-upload-file">Insert file</a>
            </div>
            
            <div class="editor-wrapper-content">
                <div class="editor-field">
                    @Html.EditorFor(model => model.Content, "TinyMCE")
                    @Html.ValidationMessageFor(model => model.Content)
                </div>
            </div>

            <div class="editor-wrapper-description">
                <div class="editor-label">
                    @Html.LabelFor(model => model.Description) (Optional)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Description)
                    @Html.ValidationMessageFor(model => model.Description)
                </div>
            </div>
            
            <div class="editor-wrapper-checkboxes">
                <div class="editor-wrapper-ispublished">
                    <div class="editor-label">
                        @Html.LabelFor(model => model.IsPublished)
                    </div>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.IsPublished)
                        @Html.ValidationMessageFor(model => model.IsPublished)
                    </div>
                </div>

                <div class="editor-wrapper-isfrontpage">
                    <div class="editor-label">
                        @Html.LabelFor(model => model.IsFrontPage)
                    </div>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.IsFrontPage)
                        @Html.ValidationMessageFor(model => model.IsFrontPage)
                    </div>
                </div>
            
                <div class="editor-wrapper-showinlist">
                    <div class="editor-label">
                        @Html.LabelFor(model => model.ShowInList)
                    </div>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.ShowInList)
                        @Html.ValidationMessageFor(model => model.ShowInList)
                    </div>
                </div>
            </div>


            <p class="submit-wrapper">
                <input type="submit" value="Save" />
            </p>

        </div>
        
        <div class="edit-content-right">

            <div class="editor-wrapper-author">
                <div class="editor-label">
                    @Html.LabelFor(model => model.AuthorId)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(x => x.AuthorId, Model.Authors)
                    @Html.ValidationMessageFor(model => model.AuthorId)
                </div>
            </div>
        
            <div class="editor-wrapper-publicationdate">
                <div class="editor-label">
                    @Html.LabelFor(model => model.PublicationDate)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.PublicationDate)
                    @Html.ValidationMessageFor(model => model.PublicationDate)
                </div>
            </div>

            <div class="editor-wrapper-keywords">
                <div class="editor-label">
                    @Html.LabelFor(model => model.Keywords)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Keywords)
                    @Html.ValidationMessageFor(model => model.Keywords)
                </div>
            </div>

            <div class="editor-wrapper-parentid">
                <div class="editor-label">
                    @Html.LabelFor(model => model.ParentId)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(x=>x.ParentId, Model.Pages, "Select")
                </div>
            </div>

        </div>

        
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

<script type="text/javascript">

    function CheckPublishRoles () {
        
        var hasPublishOwn = @(User.IsInRole(Role.PublishOwnPages)?"true":"false");;
        var hasPublishOtherUsers = @(User.IsInRole(Role.PublishOtherUsersPages)?"true":"false");
        var userName = '@(User.Identity.Name)';
        var currentPublishedValue = @(Model.Id.HasValue && Model.IsPublished ? "true" : "false");

        var publishedField = $('#@(Html.FieldIdFor(x=> x.IsPublished))');
        var newAuthor = $('#@(Html.FieldIdFor(x=> x.AuthorId)) :selected').text();
        
        if (!currentPublishedValue) {
            if (!(newAuthor != userName && hasPublishOtherUsers) && !(newAuthor == userName && hasPublishOwn)) {
                publishedField.attr('disabled', true);
                publishedField.prop('checked', false);
            }
            else {
                publishedField.attr('disabled', false);
            }
        }
    }
    
    CheckPublishRoles();
    
    $('#@(Html.FieldIdFor(x=> x.AuthorId))').change(CheckPublishRoles);

    $('#extract-from-title').click(function() {
        var callUrl = '@(Url.Action(MVC.Admin.Page.GenerateSlug()))';
        var postData = JSON.stringify({ text: $('#@Html.FieldIdFor(x=>x.Title)').val() });

        $.ajax({
                type: "POST",
                contentType: 'application/json; charset=utf-8',
                url: callUrl,
                data: postData,
                success: function(response) {
                    $('#@Html.FieldIdFor(x=>x.Slug)').val(response);
                },
                error: function() {
                    alert('Error');
                }
            });
    });

</script>